package data;


import clsaa.AllBiglotto;
import clsaa.Biglotto;
import com.google.gson.Gson;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.swing.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import static data.FileINOUT.FileSaveToJs;
import static frame.FileChooserDemo.selectFile;
import static frame.MainFrame.initpanelTable;
import static java.sql.Types.NUMERIC;
import static jdk.internal.org.objectweb.asm.Type.BOOLEAN;
import static org.apache.poi.ss.usermodel.DataValidationConstraint.ValidationType.FORMULA;
import static org.apache.xmlbeans.impl.piccolo.xml.Piccolo.STRING;

public class DataInXlsx {

    public static AllBiglotto ReadExcelInFile(String path) {
//        Read_EXCEL sheet1 = new Read_EXCEL("E:\\文件\\大乐透历史.xlsx", "Sheet1");
        Read_EXCEL sheet1 = new Read_EXCEL(path, "Sheet1");
        //获取第二行第4列
//        String cell2 = sheet1.getExcelDateByIndex(1, 1);
        AllBiglotto allbiglotto=new AllBiglotto();
        int TheEXCELrows=sheet1.The_Read_EXCEL_rows();
       for(int i=0;i<TheEXCELrows;++i){
//           for(int j=0;j<9;++j){
////               String cell=sheet1.getExcelDateByIndex(i, j);
////               System.out.print(cell+"  ");
////           }
////           System.out.println();
           String id=sheet1.getExcelDateByIndex(i, 0);
           id=id.substring(0,id.length()-2);
           String date=sheet1.getExcelDateByIndex(i, 1);
           String A=sheet1.getExcelDateByIndex(i, 2);
           A=A.substring(0,A.length()-2);
           String B=sheet1.getExcelDateByIndex(i, 3);
           B=B.substring(0,B.length()-2);
           String C=sheet1.getExcelDateByIndex(i, 4);
           C=C.substring(0,C.length()-2);
           String D=sheet1.getExcelDateByIndex(i, 5);
           D=D.substring(0,D.length()-2);
           String E=sheet1.getExcelDateByIndex(i, 6);
           E=E.substring(0,E.length()-2);
           String F=sheet1.getExcelDateByIndex(i, 7);
           F=F.substring(0,F.length()-2);
           String G=sheet1.getExcelDateByIndex(i, 8);
           G=G.substring(0,G.length()-2);
           Biglotto biglotto=new Biglotto(id,date,A,B,C,D,E,F,G);
           allbiglotto.add(biglotto);
       }
        return allbiglotto;
    }

    //测试方法
    public static void DataInXlsxfun(){
        String path=selectFile();
        if(path.equals(""))return;
        AllBiglotto allbiglotto=ReadExcelInFile(path);
        allbiglotto.reorderDescend();
        for(int i=0;i<allbiglotto.getAllData().size()-1;++i){
            for(int j=i+1;j<allbiglotto.getAllData().size();++j){
                if(allbiglotto.getAllData().get(i).getId()==allbiglotto.getAllData().get(j).getId()){
                    JOptionPane.showMessageDialog(null, "存在相同期数");
                    return;
                }
            }
        }
        Gson gson=new Gson();
        String biglottoDataJson=gson.toJson(allbiglotto);
        FileSaveToJs(biglottoDataJson,"config/AllBiglottoData.json");
        initpanelTable();
    }
}
